<?php
/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

require_once ('data/Trie.php');

/**
 * Autoloads classes.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 * @package Simplify_Kernel
 */
class ClassLoader
{

  /**
   *
   * @var string
   */
  const COMPONENT = 'Component';

  /**
   *
   * @var string
   */
  const CONTROLLER = 'Controller';

  /**
   *
   * @var string
   */
  const MODEL = 'Model';

  /**
   *
   * @var string
   */
  const HELPER = 'Helper';

  /**
   *
   * @var array
   */
  protected static $defpaths = array();

  /**
   *
   * @var array
   */
  public static $classpath;

  /**
   *
   * @var array
   */
  protected static $typesufix = array(
    'Model' => '', 'Component' => 'Component', 'Controller' => 'Controller', 'Helper' => 'Helper' 
  );

  /**
   *
   * @param $type string
   * @return string
   */
  public static function getDefaultPath($type)
  {
    return self::hasDefaultPath($type) ? self::$defpaths[$type] : '';
  }

  /**
   *
   * @param $type string
   * @return boolean
   */
  public static function hasDefaultPath($type)
  {
    return isset(self::$defpaths[$type]);
  }

  /**
   *
   * @param $type string
   * @param $path string
   * @return void
   */
  public static function setDefaultPath($type, $path)
  {
    self::$defpaths[$type] = sy_fix_path($path);
  }

  /**
   *
   * @param $name string
   * @param $path string
   * @param $type string
   * @return void
   */
  public static function import($name, $path = null, $type = '')
  {
    if (empty(self::$classpath))
      self::$classpath = new Trie();
    
    $classes = ( array ) $name;
    
    foreach ($classes as $class) {
      $class = $class . self::$typesufix[$type];
      $file = Inflector::underscore($class);
      
      if (! empty($type) && empty($path) && self::hasDefaultPath($type)) {
        $path = self::$defpaths[$type];
      }
      
      self::$classpath->set($class, sy_fix_path($path . '/' . $file . '.php'));
    }
  }

  /**
   *
   * @param $class string
   * @return void
   */
  public static function autoload($class)
  {
    if (empty(self::$classpath))
      self::$classpath = new Trie();
    
    try {
      require_once (self::$classpath->get($class));
    }
    catch (NotFoundException $e) {
      throw new NotFoundException('Class not found');
    }
  }
}

?>